const { defineConfig } = require('@vue/cli-service')
const path = require('path')

module.exports = defineConfig({
  // 打包路径
  publicPath: '/',
  // 打包后的文件夹名称
  outputDir: 'dist',
  // 静态资源目录
  assetsDir: 'static',
  // 是否需要对依赖进行转译
  transpileDependencies: true,
  // 生产环境是否需要生成sourceMap
  productionSourceMap: false,
  // 是否需要开启eslint
  lintOnSave: false,
  /** 配置插件 */
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'scss',
      // 哪些文件自动导入，使用绝对路径
      patterns: [path.resolve(__dirname, './src/styles/variable.scss')]
    }
  },
  devServer: {
    proxy: {
      [process.env.VUE_APP_BASE_API]: {
        target: 'http://127.0.0.1/api',
        // 是否跨域
        changeOrigin: true,
        // 控制台输出
        overlay: {
          warnings: false,
          errors: true
        },
        pathRewrite: {
          ['^' + process.env.VUE_APP_BASE_API]: ''
        }
      }
    }
  },
  configureWebpack: {
    name: process.env.VUE_APP_TITLE || 'vue2模板',
    // 配置路径别名
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '@components': path.resolve(__dirname, 'src/components'),
        '@assets': path.resolve(__dirname, 'src/assets')
      }
    }
  },
  css: {
    loaderOptions: {
      sass: {
        sassOptions: { outputStyle: 'expanded' }
      }
    }
  },
  chainWebpack(config) {
    config.when(process.env.NODE_ENV !== 'development', config => {
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial'
          },
          commons: {
            name: 'chunk-commons',
            test: path.resolve(__dirname, 'src/components'),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true
          }
        }
      })
      config.optimization.runtimeChunk('single')
    })
  }
})
